﻿Imports System.Data.SqlClient

Public Class admedituser

    Inherits System.Web.UI.Page
    Private sSQL As String
    Private role As String
    Private objrd As SqlDataReader = Nothing
    Private objfind As New GetData
    Private ps As New PreparedStatement(sSQL)
    Dim strErr As String = ""
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim strVID, active As String
        If (Session("admin_ID") <> Nothing And Session("admin_ID") <> "") Then
            strVID = Trim(Request("aid"))
            ps.addParameter("id", strVID)
            If Request("btnUpdate") <> Nothing And Request("btnUpdate") = "Update" Then
                Exit Sub
            End If
            sSQL = "select * from adm_user where GAPortal_ID=@id"
            ps.strSql = sSQL
            Dim conn As SqlConnection = objfind.getConn()

            objrd = objfind.dr(ps, strErr, conn, objrd)
            If objrd.Read() Then
                sUID.Text = objrd("adminuser_id").ToString
                sVID.Text = strVID
                firstname.Text = objrd("first_name").ToString
                lastname.Text = objrd("last_name").ToString
                location.Text = objrd("location").ToString
                Supervisor.Text = objrd("supervisor").ToString
                Phone.Text = objrd("phone").ToString
                Ext.Text = objrd("extension").ToString
                Email.Text = objrd("email_address").ToString
                active = objrd("active").ToString
                role = objrd("role_ID").ToString
                If active = 1 Then
                    ch.Checked = True
                Else
                    ch.Checked = False
                End If
            End If
            sSQL = "select * from user_roles"
            ps.strSql = sSQL
            objrd = objfind.dr(ps, strErr, conn, objrd)
            While (objrd.Read())
                ro.Items.Add(New ListItem(
                 objrd("label").ToString(), objrd("role_ID").ToString()))
            End While
            ro.SelectedValue = role
            objfind.closeConn(conn)

        Else
            Response.Redirect("../adm_login.aspx")
        End If
    End Sub
    Protected Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
        If firstname.Text = "" Then
            result.InnerHtml = ""
            result.InnerHtml = "<center><b><font color=""#FF0000""> Pleas enter your first name</font></b></center>"
            Exit Sub
        End If
        If lastname.Text = "" Then
            result.InnerHtml = ""
            result.InnerHtml = "<center><b><font color=""#FF0000""> Pleas enter your last name</font></b></center>"
            Exit Sub
        End If
        If Email.Text = "" Then
            result.InnerHtml = ""
            result.InnerHtml = "<center><b><font color=""#FF0000""> Pleas enter your email address</font></b></center>"
            Exit Sub
        End If
        If Supervisor.Text = "" Then
            result.InnerHtml = ""
            result.InnerHtml = "<center><b><font color=""#FF0000""> Pleas enter your supervisor</font></b></center>"
            Exit Sub
        End If
        Dim act As String
        Dim roles As String
        If ch.Checked = True Then
            act = 1
        Else
            act = 0
        End If
        roles = ro.SelectedValue
        sSQL = "update adm_user set first_name=@firstname,last_name=@lastname,location=@location,supervisor=@supervisor,phone=@phone,extension=@extension"
        sSQL = sSQL & ",email_address=@email,active=@active,role_ID=@role where GAPortal_ID=@id"
        ps.strSql = sSQL
        ps.addParameter("role", roles)
        ps.addParameter("firstname", firstname.Text)
        ps.addParameter("lastname", lastname.Text)
        ps.addParameter("phone", Phone.Text)
        ps.addParameter("email", Email.Text)
        ps.addParameter("supervisor", Supervisor.Text)
        ps.addParameter("active", act)
        ps.addParameter("extension", Ext.Text)
        ps.addParameter("location", location.Text)
        objfind.es(ps, strErr)
        If strErr = Nothing Or strErr = "" Then
            result.InnerHtml = ""
            result.InnerHtml = "<center><b><font color=""#FF0000""> Update Success！</font></b></center>"
        Else
            result.InnerHtml = ""
            result.InnerHtml = "<center><b><font color=""#FF0000"">Error！</font></b><p>" & strErr & "</p></center>"
        End If

    End Sub

    Protected Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        Response.Redirect("admfindusers.aspx")
    End Sub
End Class